package com.fingard.dsp.bank.directbank.yeepay01;

import java.io.BufferedReader;
import java.io.File;
import java.io.StringReader;
import java.util.HashMap;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IStatement;
import com.fingard.dsp.bank.reqrespobj.statement.RefundState;
import com.fingard.dsp.bank.reqrespobj.statement.ReqQryStatement;
import com.fingard.dsp.bank.reqrespobj.statement.RetAtsStatement;
import com.fingard.dsp.bank.reqrespobj.statement.TransState;
import com.fingard.dsp.bank.reqrespobj.statement.detail.RetAtsStatementDetail;
import com.fingard.text.SplitHelper;
import com.fingard.util.FtpUtil;

public class YEEPAY01Statement extends YEEPAY01Base implements IStatement {
	public RetAtsStatement d_Qry_Statement(ReqQryStatement p_reqQrySttm) throws Exception {
		RetAtsStatement retAts = new RetAtsStatement(p_reqQrySttm);
		
		retAts.writeStart();

		String tmpRespCode = "";
		String tmpRespInfo = "";

		try {
			String ftpHost = getBankConfig().getMapValueByKey("ftpMapping", "ftpHost");
			int ftpPort = Integer.parseInt(getBankConfig().getMapValueByKey("ftpMapping", "ftpPort"));
			
			String ftpUserName = getBankConfig().getMapValueByKey("ftpMapping", "ftpUserName");
			String ftpPassword = getBankConfig().getMapValueByKey("ftpMapping", "ftpPassword");
			
			String ftpPath = getBankConfig().getMapValueByKey("ftpMapping", "ftpPath");
			ftpPath = ftpPath + Format.Date8Format.format(p_reqQrySttm.getBusDate()) + "/";
			
			String localPath = getBankConfig().getMapValueByKey("ftpMapping", "localPath");
			String fileNamePrefix = getBankConfig().getMapValueByKey("ftpMapping", "fileNamePrefix");
			
			String fileName = fileNamePrefix + Format.Date6Format.format(p_reqQrySttm.getBusDate()) + ".txt";
			
			WriteBankLogLn2("待下载FTP文件路径: " + ftpPath + fileName);
			WriteBankLogLn2("文件保存至本地路径: " + localPath + File.separator + fileName);

			this.WriteBankLogLn("ftp信息："+ftpHost+":"+ftpPort+","+ftpUserName+","+ftpPath);
			FtpUtil.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName);

			String filePath = localPath + File.separator + fileName;
			String ftpFileContent = FtpUtil.getTextFromFile(filePath, "UTF-8");

			if (ftpFileContent.equals("")) {
				tmpRespCode = "-1";
				tmpRespInfo = "本日无交易";
			}

			WriteBankLogLn2("对账单文件内容: \r\n" + ftpFileContent);
	        
	        String[] totalLine = ftpFileContent.split("\r\n");
	        if (totalLine.length > 1) {
	        	tmpRespCode = "0";
	        	
	        	int endIndex = totalLine[0].lastIndexOf("总金额");
	        	String totalNum = totalLine[0].substring(4, endIndex).trim();
	        	
	        	int detailIndex = ftpFileContent.indexOf("流水号");
	        	String detailContent = ftpFileContent.substring(detailIndex);
	        	
	        	StringReader strReader = new StringReader(detailContent);
				BufferedReader tmpReader = new BufferedReader(strReader);
				
				tmpReader.readLine();
				String[] columns = new String[]{"流水号","终端号","交易识别号","交易日期","金额","清算账号","交易类型"};
				
				String line = "";
				while((line = tmpReader.readLine()) != null) {
					String eachline = line.trim();
					
					if(eachline == null) {
						retAts.writeRespCode("-1");
						retAts.writeRespInfo("本日或本月无收支");
						
						break;
					} else {
						if (eachline.equals("")) {
							continue;
						}
						
						String[] values = eachline.split("\\|");
						
						if(columns.length == values.length){
							HashMap<String, String> mapKV = SplitHelper.splitAsValueMap(columns, values);
							RetAtsStatementDetail retDetail = new RetAtsStatementDetail();
							
							if(mapKV.containsKey("交易日期")){
								retDetail.busTime = mapKV.get("交易日期").trim()+"000000";
							}
							
							if(mapKV.containsKey("交易类型")){
								retDetail.busType = mapKV.get("交易类型").trim();
							}
							
							if(mapKV.containsKey("流水号")){
								retDetail.askNumber = mapKV.get("流水号").trim();
							}
							
							if(mapKV.containsKey("金额")){
								retDetail.amount = mapKV.get("金额").trim();
							}
							
							if(mapKV.containsKey("终端号")){
								retDetail.termCode = mapKV.get("终端号").trim();
							}
							
							retDetail.transState = TransState.Success;
							retDetail.refundState = RefundState.Exception;
							
							retAts.writeRetDetail(retDetail);
						} else {
							break;
						}
					}
				}
				
				retAts.writeTotalNum(totalNum);
	        } else {
	        	tmpRespCode = "-1";
	        	tmpRespInfo = "返回结果不符合预期!";
	        }
		} catch (Exception ex) {
			ex.printStackTrace();
			
			tmpRespCode = "-1";
			tmpRespInfo = ex.getMessage();

			WriteBankLogLn(ex);
		}
		
		if(tmpRespCode.equals("-1")){
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		}
		
		retAts.writeEnd();
		return retAts;
	}
}
